package com.example.twotwo_project;


import java.util.Locale;

import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.ActionBarDrawerToggle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.app.FragmentPagerAdapter;
import android.content.Context;
import android.content.Intent;
import android.content.res.Configuration;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.support.v4.widget.DrawerLayout;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.FrameLayout;
import android.widget.ListView;

public class MainActivity extends ActionBarActivity implements ActionBar.TabListener {
    SectionsPagerAdapter mSectionsPagerAdapter;
    ViewPager mViewPager;
    
    String[] navItem;
    ListView lvNavlist;
    FrameLayout flContainer;
    ArrayAdapter<String> adtDrawerList;
    
    DrawerLayout dlDrawer;
    ActionBarDrawerToggle dtToggle;
    
    BodyFragment fragBody;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        fragBody = BodyFragment.newInstance();
        
        
        // 네비게이션 bar
        lvNavlist = (ListView)findViewById(R.id.lv_activity_main_nav_list);
        //flContainer = (FrameLayout)findViewById(R.id.fl_activity_main_container);
        
        navItem = getResources().getStringArray(R.array.List_nav);
        Log.i("ArrayList", "list : " + navItem.length);
                
        adtDrawerList = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, navItem);
        lvNavlist.setAdapter(adtDrawerList);
        lvNavlist.setOnItemClickListener(new AdapterView.OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				switch(position){
				case 0 :
					//getSupportFragmentManager().beginTransaction().replace(R.id.fl_activity_main_container, fragBody).commit();
					Intent intent = new Intent(getApplicationContext(), LoginActivity.class);
					startActivity(intent);
					break;
				}
				dlDrawer.closeDrawer(lvNavlist);
			}
		});
        
        dlDrawer = (DrawerLayout)findViewById(R.id.dl_activity_main_drawer);
        dtToggle = new ActionBarDrawerToggle(this, dlDrawer, R.drawable.ic_drawer, R.string.open_drawer, R.string.close_drawer);
        dlDrawer.setDrawerListener(dtToggle);
        
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        
        
        final ActionBar actionBar = getSupportActionBar();
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

        mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());

        mViewPager = (ViewPager) findViewById(R.id.pager);
        mViewPager.setAdapter(mSectionsPagerAdapter);
        

        mViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
            @Override
            public void onPageSelected(int position) {
                actionBar.setSelectedNavigationItem(position);
                
            }
        });


        for (int i = 0; i < mSectionsPagerAdapter.getCount(); i++) {
            actionBar.addTab(
                    actionBar.newTab()
                            .setText(mSectionsPagerAdapter.getPageTitle(i))
                            .setTabListener(this));
        }
        
        
    }

    @Override
	public void onConfigurationChanged(Configuration newConfig) {
		// TODO Auto-generated method stub
		super.onConfigurationChanged(newConfig);
		dtToggle.onConfigurationChanged(newConfig);
	}




	@Override
	protected void onPostCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onPostCreate(savedInstanceState);
		dtToggle.syncState();
	}




	@Override
    public boolean onCreateOptionsMenu(Menu menu) {

        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
    	
    	Intent intent = new MyOptionMenu(getApplicationContext(), item).getIntent();
    	startActivity(intent);
    	
    	/*Intent intent = null;
    	
        int id = item.getItemId();
        switch(id){
        case R.id.action_map:
        	intent = new Intent(getApplicationContext(),MapView.class);
        	startActivity(intent);
        }*/
        /*if (id == R.id.action_settings) {
            return true;
        }*/
    	if(dtToggle.onOptionsItemSelected(item)){
    		return true;
    	}
    	
    	
        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onTabSelected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
        // When the given tab is selected, switch to the corresponding page in
        // the ViewPager.
        mViewPager.setCurrentItem(tab.getPosition());
    }

    @Override
    public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
    }

    @Override
    public void onTabReselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
    }


    public class SectionsPagerAdapter extends FragmentPagerAdapter {
    	Context mContext;
    	
        public SectionsPagerAdapter(FragmentManager fm) {
            super(fm);
        }


        
		@Override
		public Fragment getItem(int position) {

			switch(position) {
			case 0:
				
				return new Frag01Activity(mContext);
			case 1:
				/*ActivityGroup activityGroup = new ActivityGroup();
				LocalActivityManager localActivityManager = activityGroup.getLocalActivityManager();
				Intent intent = new Intent(mContext, MapView.class);
				Window window = localActivityManager.startActivity("hosted", intent);
				View view = window.getDecorView()*/
				
				return new Frag02Activity(mContext);
			case 2:
				return new Frag03Activity(mContext);
			}
			return null;
		}

        @Override
        public int getCount() {
            // Show 3 total pages.
            return 3;
        }

        @Override
        public CharSequence getPageTitle(int position) {
            Locale l = Locale.getDefault();
            switch (position) {
                case 0:
                    return getString(R.string.title_section1).toUpperCase(l);
                case 1:
                    return getString(R.string.title_section2).toUpperCase(l);
                case 2:
                    return getString(R.string.title_section3).toUpperCase(l);
            }
            return null;
        }
    }


    public static class PlaceholderFragment extends Fragment {

        private static final String ARG_SECTION_NUMBER = "section_number";

        public static PlaceholderFragment newInstance(int sectionNumber) {
            PlaceholderFragment fragment = new PlaceholderFragment();
            Bundle args = new Bundle();
            args.putInt(ARG_SECTION_NUMBER, sectionNumber);
            fragment.setArguments(args);
            return fragment;
        }

        public PlaceholderFragment() {
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.fragment_main, container, false);
            return rootView;
        }
    }

}
